﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.DTOs;
using static Butcher.winform.EnterpriseForms.DeductionHtml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class ScaleList : Form
    {
        public ScaleList()
        {
            InitializeComponent();
            textBox2.Text = "";
            ScaleSearch();
        }
        public class ScaleListHtml
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? Name { get; set; }
        };
        ScaleListHtml scalestr = new ScaleListHtml();
        public string Name = "";
        public int pageIndex = 1;
        public int pageSize = 5;
        public int pageCount = 0;
        public int totalCount = 0;
        public void ScaleSearch()
        {
            string url = @"http://localhost:5229/api/Section/ScaleDisplayHtml";
            HttpClient client = new HttpClient();
            scalestr.pageIndex = pageIndex;
            scalestr.pageSize = pageSize;
            scalestr.Name = textBox2.Text;
            var content = new StringContent(JsonSerializer.Serialize<ScaleListHtml>(scalestr), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["lists"].Value<JArray>();
            var counts = josnresult["totalCount"];
            var pages = josnresult["pageCount"];
            totalCount = Convert.ToInt32(counts);
            pageCount = Convert.ToInt32(pages);
            List<ScaleSelectListDTO> sss = results.ToObject<List<ScaleSelectListDTO>>();
            dataGridView1.DataSource = sss;
            uiTextBox1.Text = pageIndex.ToString();
            uiTextBox2.Text = totalCount.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != null)
            {
                Name = textBox2.Text;
            }

            ScaleSearch();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (pageIndex == 1)
            {
                MessageBox.Show("到首页了");
            }
            else
            {
                pageIndex -= 1;
            }
            ScaleSearch();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (pageIndex == totalCount)
            {
                MessageBox.Show("到末页了");
            }
            else
            {
                pageIndex += 1;
            }
            ScaleSearch();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            ScaleSearch();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            pageIndex = totalCount;
            ScaleSearch();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ScaleSearch();
        }
    }
}
